package com.example.h5server.service;


import com.alibaba.excel.EasyExcel;
import com.example.h5server.model.EmployeeData;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@Service
public class ExcelExportService {

    public void writeEmployeeExcelToResponse(List<EmployeeData> dataList, HttpServletResponse response, String fileName)
            throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String encodedFileName = URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + encodedFileName);

        EasyExcel.write(response.getOutputStream(), EmployeeData.class)
                .sheet("员工信息")
                .doWrite(dataList);
    }

    // 保留原有方法以保证向后兼容
    public void writeEmployeeExcelToResponse(List<EmployeeData> dataList, HttpServletResponse response)
            throws IOException {
        writeEmployeeExcelToResponse(dataList, response, "员工信息.xlsx");
    }
}